package com.mingqijia.gassafety.webserver.service.impl;
import java.time.ZoneId;
import java.util.Date;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mingqijia.gassafety.db.entity.Dtu;
import com.mingqijia.gassafety.db.entity.RecordDetector;
import com.mingqijia.gassafety.db.influxdb.config.InfluxDBConfiguration;
import com.mingqijia.gassafety.db.influxdb.config.InfluxDBConnect;
import com.mingqijia.gassafety.shared.constant.EquipmentType;
import com.mingqijia.gassafety.shared.constant.EquipmentTypeConst;
import com.mingqijia.gassafety.webserver.response.client.ConcentrationCurveRes;
import com.mingqijia.gassafety.webserver.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 报警记录 服务实现类
 * </p>
 *
 * @author guoliang.lao
 * @since 2022-03-04
 */
@Service
@Slf4j
public class HistoryLogServiceImpl implements HistoryLogService {

    @Autowired
    EquipmentService equipmentService;
    @Autowired
    RecordDetectorService recordDetectorService;
//    @Autowired
//    InfluxDBConfiguration influxDBConfiguration;

    @Override
    public List<ConcentrationCurveRes> getConcentrationCurve(String imei, String timeStart, String timeEnd) {
        List<ConcentrationCurveRes> result = new ArrayList<>();

        LambdaQueryWrapper<Dtu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dtu::getIMEI,imei).eq(Dtu::getIsDeleted,0);
        Dtu equipment = equipmentService.getOne(lambdaQueryWrapper);
        if (equipment == null) return result;
        //控制器没有浓度曲线
        if (equipment.getEquipmentType().equals(EquipmentType.GAS_ALARM_CONTROLLER.getCode())) return result;

        LambdaQueryWrapper<RecordDetector> detectorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        detectorLambdaQueryWrapper.eq(RecordDetector::getIsDeleted,0)
                .eq(RecordDetector::getConsumerId,equipment.getConsumerId())
                .eq(RecordDetector::getDeviceId,equipment.getId())
                .ge(RecordDetector::getRecordAt,timeStart)
                .le(RecordDetector::getRecordAt,timeEnd);
        detectorLambdaQueryWrapper.orderByAsc(RecordDetector::getRecordAt);
        List<RecordDetector> list = recordDetectorService.list(detectorLambdaQueryWrapper);
        log.info("探头结果：{}"+list);
        for (RecordDetector recordDetector : list) {
            ConcentrationCurveRes res = new ConcentrationCurveRes();
            res.setTime(recordDetector.getRecordAt());
            if (recordDetector.getConcentration() == null) {
                res.setValue(0.0);
            } else {
                res.setValue(Double.valueOf(recordDetector.getConcentration()));
            }
            result.add(res);
        }
//        //获取数据库连接
//        InfluxDBConnect influxDBConnect = influxDBConfiguration.getInfluxDBConnect();
//
//        StringBuilder sql = new StringBuilder("select time,CoreValue from DataLogAsDetector where");
//        sql.append(" time>='" + timeStart+"'");
//        sql.append(" and time<='" + timeEnd+"'");
//        sql.append(" and Path='" + equipment.getPath()+"'");
//        String dtuId = "";
//        if (equipment.getEquipmentType().equals(EquipmentType.POINT_TYPE_GAS_DETECTOR.getCode())) {
//            dtuId = org.apache.commons.lang.StringUtils.substringBefore(equipment.getIMEI(), "-");
//        } else {
//            dtuId = imei;
//        }
//        sql.append(" and DtuId='" + dtuId +"'");
//        String sub = "";
//        if (equipment.getEquipmentType().equals(EquipmentType.POINT_TYPE_GAS_DETECTOR.getCode())) {
//            sub = org.apache.commons.lang.StringUtils.substringAfter(equipment.getIMEI(), "-");
//        } else {
//            sub = "1-1";
//        }
//        sql.append(" and SubIdentifier='" + sub +"'");
//        sql.append(" order by time desc");
//        log.info(sql.toString());
//        QueryResult queryResult = influxDBConnect.query(sql.toString());
//
//        List<List<Object>> list = new ArrayList<>();
//        if (queryResult.getResults().get(0).getSeries() != null) {
//            list = queryResult.getResults().get(0).getSeries().get(0).getValues();
//        }
//        for (List<Object> objects : list) {
//            ConcentrationCurveRes res = new ConcentrationCurveRes();
//            res.setTime(DateUtil.parse(objects.get(0).toString()));
//            res.setValue(Double.valueOf(objects.get(1).toString()));
//            result.add(res);
//        }
        return result;
    }

    public static void main(String[] args) {
        String imei = "123123123-1-134";
        String sub = StringUtils.substringAfter(imei, "-1-");
        System.out.println(sub);
    }
}
